<!-- 新增业务员 -->
<template>
  <div class="new-salesman-dialog">
    <el-dialog
      width="800px"
      class=""
      top="6vh"
      title="新增"
      :visible="$store.state.dialog.newSalesman"
      @close="close">
      <el-row>
        <el-col :span="24">
          <el-card shadow="hover">
            <el-form :model="newData" label-width="100px" size="mini" :rules="rules" ref="newData">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="中文名" prop="fullName">
                    <el-input v-model="newData.fullName" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="newData.sex" auto-complete="off">
                      <el-radio label="1">男</el-radio>
                      <el-radio label="2">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="QQ" prop="qq">
                    <el-input v-model="newData.qq" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="电话号码" prop="phone">
                    <el-input v-model="newData.phone" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="手机号码" prop="mobile">
                    <el-input v-model="newData.mobile" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="部门" prop="jobId">
                    <el-select placeholder="请选择部门" v-model="newData.jobId" auto-complete="off">
                      <el-option v-for="item in jobList" :key="item.jobId" :label="item.jobName" :value="item.jobId"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="状态">
                    <el-select placeholder="请选择" v-model="newData.state" auto-complete="off">
                      <el-option label="激活" value="1"></el-option>
                      <el-option label="未激活" value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="登录用户名" prop="userName">
                    <el-input v-model="newData.userName" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="密码" prop="password">
                    <el-input v-model="newData.password" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="确认密码" prop="makeSurePassword">
                    <el-input v-model="newData.makeSurePassword" auto-complete="off"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="loadShow" @click="sureAddsalesman('newData')" size="mini">确 定</el-button>
        <el-button type="primary" size="mini" v-else :loading="true">加载中</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSalesman } from '../api/userman'
export default {
  data () {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号码不能为空'))
      } else {
        const reg = /^([1-9]\d{10})$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号码'))
        }
      }
    }
    // var checkPhone2 = (rule, value, callback) => {
    //   const reg = /0\d{2}-\d{7,8}/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的座机号'))
    //   }
    // }
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      } else {
        const reg = /^[a-zA-Z0-9_-]{4,16}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入长度在 4 到 16 位规范的用户名'))
        }
      }
    }
    var checkPassworder = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('密码不能为空'))
      } else {
        const reg = /^[a-zA-Z0-9]{6,11}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入长度在 6 到 11 个规范的密码'))
        }
      }
    }
    var surePassworder = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.newData.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var checkQQ = (rule, value, callback) => {
      const reg = /^[1-9][0-9]{4,9}$/
      if (reg.test(value) || value === '') {
        callback()
      } else {
        return callback(new Error('请输入正确的QQ号'))
      }
    }
    return {
      newData: {
        userName: '',
        password: '',
        makeSurePassword: '',
        mobile: '',
        fullName: '',
        sex: '',
        userType: 8,
        state: '',
        qq: '',
        phone: '',
        jobName: '',
        jobId: ''
      },
      loadShow: true,
      rules: {
        fullName: [
          { required: true, message: '请输入中文姓名', trigger: 'blur' },
          { min: 2, max: 4, message: '长度在 2 到 4 个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, validator: checkUsername, trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        jobId: [
          { required: true, message: '请选择部门', trigger: 'blur' }
        ],
        mobile: [
          { required: true, validator: checkPhone, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: checkPassworder, trigger: 'blur' }
        ],
        makeSurePassword: [
          { required: true, validator: surePassworder, trigger: 'blur' }
        ],
        qq: [
          { validator: checkQQ, trigger: 'blur' }
        ]
        // phone: [
        //   { validator: checkPhone2, trigger: 'blur' }
        // ]
      }
    }
  },
  props: {
    jobList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    // 新增
    async newDataTab (formName) {
      this.loadShow = false
      let that = this
      const res = await addSalesman(this.newData)
      if (res.status === 200) {
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        setTimeout(function () {
          that.loadShow = true
        }, 500)
        this.$parent.getTabeDate(1, 15)
        this.resetForm(formName)
        this.$store.commit('hide_new_salesman_dialog')
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
        this.loadShow = true
      }
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    sureAddsalesman (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.newDataTab(formName)
        } else {
          return false
        }
      })
    },
    close () {
      this.$store.commit('hide_new_salesman_dialog')
    }
  }
}
</script>

<style scoped lang="less">

</style>
